package cn.shyouchi.wuguanjia.android.ui.event.labor;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/21 at 10:55
 * Email: 472279981@qq.com
 * Des:
 */

public class LaborPresenter implements LaborContract.Presenter {

    Context mContext;
    EventApi mEventApi;
    LaborContract.View mView;

    public LaborPresenter(Context context, LaborContract.View view) {
        mEventApi = new EventApi(context);
        this.mContext = context;
        this.mView = view;
    }

    @Override
    public void getRankList(String projectId) {
        mEventApi.gethoursTimeList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<RankTime, List<RankTime.ResultMessageBean>>() {
                    @Override
                    public List<RankTime.ResultMessageBean> call(RankTime rankTime) {
                        return rankTime.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<RankTime.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<RankTime.ResultMessageBean> resultMessageBeen) {
                        mView.onRankRefresh(resultMessageBeen);
                    }
                });
    }

    @Override
    public void getLaborList(String projectId,String date) {
        mEventApi.getLaborList(projectId, date)
                .subscribeOn(Schedulers.io())
                .map(new Func1<LaborRequestEntity, List<LaborRequestEntity.ResultMessageBean>>() {
                    @Override
                    public List<LaborRequestEntity.ResultMessageBean> call(LaborRequestEntity laborRequestEntity) {
                        return laborRequestEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<LaborRequestEntity.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<LaborRequestEntity.ResultMessageBean> resultMessageBeen) {
                        mView.onDataRefresh(resultMessageBeen);
                    }
                });
    }

    @Override
    public void addLabor(final String projectId, List<LaborEntity> laborEntities) {
        Observable.from(laborEntities)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<LaborEntity, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(LaborEntity entity) {
                        return mEventApi.addLabor(projectId, entity);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        mView.onDateChange();
                    }
                });
    }






    public void upLabor(final String projectId, final String id, List<LaborEntity> laborEntities) {
        Observable.from(laborEntities)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<LaborEntity, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(LaborEntity entity) {
                        return mEventApi.upLabor(projectId, id, entity);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        mView.onDateChange();
                    }
                });
    }


//    public void getApplyLborList(final String projectId, String date) {
//
//    }

    @Override
    public void delLabor(String projectId, String id) {
        mEventApi.delLabor(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        mView.onDateChange();
                    }
                });
    }

    @Override
    public void attachView(@NonNull LaborContract.View view) {

    }

    @Override
    public void detachView() {

    }
}
